package org.swidgets.base.spanels;

import org.swidgets.base.BorderStyle;
import org.swidgets.base.HTMLInjector;
import org.swidgets.base.slabel.SLabel;
import com.google.gwt.user.client.ui.Grid;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Panel;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.Widget;

public class SFrame extends Grid{
	private Panel mainPanel= new SimplePanel();; 
	protected  SLabel labelCaption=new SLabel();
	protected HorizontalPanel head  = new HorizontalPanel();
	
	public SFrame(){
		super(5,3);
		
		RowFormatter rf=getRowFormatter();
		rf.setVisible(1, false);
		rf.setVisible(3, false);
		
		setBorderWidth(0);
		setCellPadding(0);
		setCellSpacing(0);
		labelCaption.setStyle(SLabel.TITLE_LABEL_STYLE);
		
		//setWidget(0, 1, labelCaption);	
		setWidget(0, 1, head);
		head.add(labelCaption);
		CellFormatter cf=getCellFormatter();
		cf.setStyleName(0, 0, "tableCorner");
		cf.setStyleName(0, 2, "tableCorner");
		cf.setStyleName(4, 0, "tableCorner");
		cf.setStyleName(4, 2, "tableCorner");
		this.addStyleName("fontZero");
	}
	
	
	public void setStyle(BorderStyle style){		
		setStyleName(style.getMain());
		String left= style.getLeft();
		String right= style.getRight();
		CellFormatter cf=getCellFormatter();
		cf.setStyleName(0, 1, style.getTop());
		cf.setStyleName(4, 1, style.getBottom());
		cf.setStyleName(1, 0, left);
		cf.setStyleName(2, 0, left);
		cf.setStyleName(3, 0, left);
		cf.setStyleName(1, 2, right);
		cf.setStyleName(2, 2, right);
		cf.setStyleName(3, 2, right);
		cf.setStyleName(1, 1, style.getHeader());
		cf.setStyleName(2, 1, style.getContent());
		cf.setStyleName(3, 1, style.getFooter());		
//		
//		//setting corners imgs
//
		HTMLInjector.imgInject(cf.getElement(0, 0), style.getUrlImgTopLeft());
		HTMLInjector.imgInject(cf.getElement(0, 2), style.getUrlImgTopRight());
		HTMLInjector.imgInject(cf.getElement(4, 0), style.getUrlImgBottomLeft());
		HTMLInjector.imgInject(cf.getElement(4, 2), style.getUrlImgBottomRight());

		this.addStyleName("fontZero");

	}

	public void add(Widget child) {
		mainPanel.add(child);
		this.setWidget(2, 1, mainPanel);		
	}

	public void addMessage(Widget message) {
		if (head.getWidgetCount()==2 ) 
			head.remove(1);
		
		head.add(message);
	}

	public void setBodyWidget(Widget w){
		this.setWidget(2, 1, w);
	}
	
	public void setCaption(String title){
		this.labelCaption.setText(title);
	}
		
	public void setHeaderWidget(Widget w){
		this.setWidget(1, 1, w);
	}
	
	public void setFooterWidget(Widget w){
		this.setWidget(3, 1, w);
	}
	
	public boolean isHasHeader(){
		return this.getRowFormatter().isVisible(1);
	}
	public boolean isHasFooter(){
		return this.getRowFormatter().isVisible(3);
	}

	public void showHeader(){
		this.getRowFormatter().setVisible(1, true);
	}
	
	public void hideHeader(){
		this.getRowFormatter().setVisible(1, false);
	}
	
	public void showFooter(){
		this.getRowFormatter().setVisible(3, true);
	}
	
	public void hideFooter(){
		this.getRowFormatter().setVisible(3, false);
	}
	
	public void setCellStyle(int row,int column,String style){
		this.getWidget(row, column).setStyleName(style);
	}
	public void clearContent(){
		this.clearCell(2, 1);
	}
		
	public Widget getBodyWidget(){
		return this.getWidget(2, 1);
	}
	
}
